<template>
  <div class="order">
    <shop-header>  
      <h2>确认订单</h2>  
    </shop-header>
    <div class="address-defalut-wrap">
      <router-link class="address-defalut" :to="{ path: '/my/address', query: { aid: 1 } }" tag="div">
        <div v-if="userInfo.address" class="name"><strong>{{ userInfo.address.name }} {{ userInfo.address.phone }}</strong></div>
        <div v-else class="name"><strong>请填写联系方式</strong></div>
        <p v-if="userInfo.address" class="adress-detail">
          <span>{{ userInfo.address.province + userInfo.address.city + userInfo.address.county + userInfo.address.detailInfo }}</span>
        </p>
        <p v-else class="adress-detail"><span>无地址</span></p>
      </router-link>
    </div>
    <split></split>
    <div class="goods">
      <div class="goods-item bottom-1px" v-for="item in buyList">
        <div class="goods-img">
          <span class="img-holder">
            <img :src="item.goodsCover" class="obj-cover">
          </span>
        </div>
        <div class="goods-container">
          <div class="top">
            <span class="name">{{ item.goodsName }}</span>
          </div>
          <div class="bottom">
            <div class="price">¥{{ item.price | toDecimal }}</div>
            <span class="quantity">数量：{{ item.quantity }}件</span>
          </div>
        </div>
      </div>
    </div>
    <split></split>
    <!-- <group gutter="0">
      <cell class="bottom-1px" title="发表信息" value="不开发票" :border-intent="false" :arrow-direction="showContent ? 'up' : 'down'" @click.native="showContent = !showContent" is-link></cell>
      <div class="content-slide" :class="showContent?'animate':''">
        <div class="invoice">
          <div class="invoice-item bottom-1px">
            <span class="title">发票抬头</span>
            <div class="invoice-label">
              <span class="label" @click="invoiceType = 1">
                <x-icon fill="#f23030" type="ios-checkmark" v-if="invoiceType == 1" size="20"></x-icon>
                <x-icon style="fill: #999" type="ios-circle-outline" v-if="invoiceType == 2" size="20"></x-icon>
                <em>个人</em>
              </span>
              <span class="label" @click="invoiceType = 2">
                <x-icon fill="#f23030" type="ios-checkmark" v-if="invoiceType == 2" size="20"></x-icon>
                <x-icon style="fill: #999" type="ios-circle-outline" v-if="invoiceType == 1" size="20"></x-icon>
                <em>公司</em>
              </span>
            </div>
          </div>
          <div class="invoice-company" v-if="invoiceType == 2 && invoiceContent == 2">
            <div class="form-group"><input class="form-control" type="text" placeholder="公司全称"></div>
            <div class="form-group"><input class="form-control" type="text" placeholder="请填写纳税人识别号"></div>
            <div class="invoice-btn">
              <six-button type="danger">确认</six-button>
            </div>
          </div>
          <div class="invoice-item bottom-1px">
            <span class="title">发票内容</span>
            <div class="invoice-label">
              <span class="label" @click="invoiceContent = 1">
                <x-icon fill="#f23030" type="ios-checkmark" v-if="invoiceContent == 1" size="20"></x-icon>
                <x-icon style="fill: #999" type="ios-circle-outline" v-if="invoiceContent == 2" size="20"></x-icon>
                <em>不开发票</em>
              </span>
              <span class="label" @click="invoiceContent = 2">
                <x-icon fill="#f23030" type="ios-checkmark" v-if="invoiceContent == 2" size="20"></x-icon>
                <x-icon style="fill: #999" type="ios-circle-outline" v-if="invoiceContent == 1" size="20"></x-icon>
                <em>明细</em>
              </span>
            </div>
          </div>
        </div>
      </div>
    </group>
    <split></split> -->
    <group gutter="0">
      <x-textarea v-model="message" title="订单备注" placeholder="选填，给商家留言" :max="40" :rows="1" autosize></x-textarea>
    </group>
    <split></split>
    <div class="buy-section">
      <ul class="buy-chart">
        <li class="buy-chart-item">
          <p class="buy-chart-item-text">商品金额</p>
          <p class="buy-chart-item-price">¥ {{ totalPrice | toDecimal }}</p>
        </li>
        <li class="buy-chart-item">
          <p class="buy-chart-item-text">运费</p>
          <p class="buy-chart-item-price">¥ 0</p>
        </li>
      </ul>
      <div class="total-price">
        总价：<span>¥{{ totalPrice | toDecimal }}</span>
      </div>
      <div class="pay_now">
        <six-button @click.native="toPayment" type="primary">在线支付</six-button>
      </div>
    </div>
  </div>
</template>

<script>
import ShopHeader from '@/components/public/ShopHeader'
import Split from '@/components/public/Split'
import Cartcontrol from '@/components/public/Cartcontrol'
import { Group, Cell, CheckIcon, XTextarea } from 'vux'
import { mapGetters, mapActions } from 'vuex'
import { toDecimal, removeStore } from '@/utils/mUtils'
import http from '@/utils/http'

export default {
	components: {
    ShopHeader,
    Split,
    Group,
    Cell,
    Cartcontrol,
    CheckIcon,
    XTextarea
	},
  data () {
    return {
      showContent: false,
      invoiceType: 1,
      invoiceContent: 1,
      message: ''
    }
  },
  computed: {
    ...mapGetters(['userInfo', 'buyList']),
    totalPrice () {
      let totalPrice = null
      this.buyList.forEach((item) => {
        totalPrice += item.price * item.quantity
      })
      return totalPrice
    }  
  },
  methods: {
    ...mapActions(['getCartLen']),
    toPayment () {
      let goods = [], obj = {}
      if (!this.userInfo.address) {
        this.$vux.toast.show({
          text: '请选择收货地址!',
          type: 'warn',
          width: 'auto',
          position: 'middle'
        })
        return
      }
      this.buyList.forEach(item => {
        obj = {
          goodsId: item.goodsId,
          quantity: item.quantity
        }
        if (item.skuId) {
          obj.skuId = item.skuId
        }
        goods.push(obj)
      })
      let params = {
        goods: JSON.stringify(goods),
        addr: this.userInfo.address._id,
        buyerMessage: this.message
      }
      http.post('/api/v1/buyConfirm', params, false, res => {
        if (res.status == 1) {
          // 初始化先清除缓存的buyList
          removeStore('MY_BUY_LIST')
          this.getCartLen()
          window.location.href = res.data.qr
        }else {
          this.$vux.toast.show({
            type: 'warn',
            text: res.msg,
            width: 'auto',
            position: 'middle'
          })
        }
      })
    }
  },
  filters: {
    toDecimal (val) {
      return toDecimal(val)
    }
  },
  mounted () {
    if (this.buyList.length === 0) {
      this.$router.go(-1)
    }
  },
  beforeRouteLeave (to, from, next) {
    let that = this
    if (this.buyList.length === 0) {
      next()
    }else if (to.path != '/my/adress') {
      that.$vux.confirm.show({
        title: '提示信息',
        content: '便宜不等人，请三思而行',
        confirmText: '去意已决',
        cancelText: '我再想想',
        onConfirm () {
          next()
          setTimeout(() => {
            that.$store.dispatch('clearBuyList')
          }, 500)
        }
      })
    }else {
      next()
    }
  }
}
</script>

<style scoped>
.address-defalut {
  position: relative;
  background: #fff url(../assets/images/adress_btm.png) repeat-x bottom center;
  background-size: 60px 5px; 
  padding: 15px 30px 20px 15px;
}
.address-defalut:before {
  content: " ";
  display: inline-block;
  height: 6px;
  width: 6px;
  border-width: 2px 2px 0 0;
  border-color: #C8C8CD;
  border-style: solid;
  -webkit-transform: matrix(0.71, 0.71, -0.71, 0.71, 0, 0);
  transform: matrix(0.71, 0.71, -0.71, 0.71, 0, 0);
  position: relative;
  top: -2px;
  position: absolute;
  top: 50%;
  margin-top: -4px;
  right: 17px;
}
.name {
  font-size: 14px;
  color: #333;
}
.adress-detail {
  font-size: 0;
}
.adress-detail span {
  font-size: 14px;
  color: #333;
  line-height: 18px;
  display: inline-block;
  vertical-align: middle;
}

.goods-item {
  display: flex;
  padding: 15px;
  background: #fff;
}
.goods-item:after {
  bottom: 0;
}
.goods-img {
  flex: 0 0 70px;
  width: 70px;
  height: 70px;
  margin-right: 8px;
}
.goods-img .img-holder {
  width: 70px;
  display: block;
  padding-bottom: 100%;
}
.goods-container {
  flex: 1;
}
.top,.bottom {
  display: flex;
}
.top .name {
  flex: 1;
  font-size: 14px;
  color: #333;
  font-weight: 700;
  line-height: 20px;
  height: 40px;
  overflow: hidden;
  display: -webkit-box;
  text-overflow: ellipsis;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.top .delete-goods svg {
  flex: 0 0 auto;
  fill: #999;
}
.bottom {
  padding-top: 8px;
}
.bottom .price {
  flex: 1;
  line-height: 24px;
  color: #f23030;
}
.content-slide {
  padding: 0 15px;
  overflow: hidden;
  max-height: 0;
  transition: max-height .5s cubic-bezier(0, 1, 0, 1) -.1s;
}
.animate {
  max-height: 9999px;
  transition-timing-function: cubic-bezier(0.5, 0, 1, 0);
  transition-delay: 0s;
}
.invoice-item {
  display: flex;
  padding: 10px 0;
}
.invoice-item .title {
  font-size: 14px;
  color: #333;
  flex: 0 0 auto;
  margin-right: 20px;
}
.invoice-label {
  flex: 1;
}
.label {
  margin-right: 20px;
}
.label * {
  display: inline-block;
  vertical-align: middle;
  font-style: normal;
}
.invoice-company {
  margin-top: 10px;
}
.form-group {
  padding: 0 0 10px 0;
}
.form-group input {
  width: 100%;
  background: #f7f7f7;
  outline: none;
  height: 32px;
  line-height: 32px;
  font-style: 14px;
  color: #666;
  padding: 0 15px;
}
.invoice-btn, .pay_now {
  width: 100%;
}
.invoice-btn button,
.pay_now button {
  color: #fff;
  width: 100%;
  height: 42px;
  line-height: 42px;
}
.buy-section {
  background: #fff;
  padding: 10px 15px 20px 15px;
}
.buy-chart-item {
  display: flex;
  padding-bottom: 3px;
}
.buy-chart-item-text {
  flex: 1;
}
.buy-chart-item-price {
  color: #e93b3d;
}
.total-price {
  text-align: right;
  font-size: 16px;
  font-weight: 700;
}
.total-price span {
  color: #e93b3d;
  font-size: 16px;
  font-weight: 400;
}
.pay_now {
  padding-top: 15px;
}
.quantity {
  color: #999;
}
</style>
